package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import control.*;
import model.*;
import java.awt.Font;
import javax.swing.SwingConstants;


public class CustomerMenu extends JFrame {
	
	private JPanel contentPane;
	private static MainMenu mm = new MainMenu();
	private static CustomerGroupCtrl customerGrCtrl = new CustomerGroupCtrl();
	private static CustomerCtrl customerCtrl = new CustomerCtrl();
	//CustomerGroup
	private JTextField txtCgroupType;
	private JTextField txtCid;
	private JTextField txtCzipCode;
	private JTextField txtCdiscount;
	//Customer
	private JTextField txtCfname;
	private JTextField txtCcity;
	private JTextField txtClname;
	private JTextField txtCaddress;
	private JTextField txtCid2;
	private JTextField txtCgroupId2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CustomerMenu frame = new CustomerMenu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CustomerMenu() {
		setTitle("Customer Menu");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 640, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 624, 312);
		contentPane.add(tabbedPane);
		
		JPanel customerGroupPanel = new JPanel();
		tabbedPane.addTab("Customer Group", null, customerGroupPanel, null);
		customerGroupPanel.setLayout(null);
		
		JPanel customerPanel = new JPanel();
		tabbedPane.addTab("Customer", null, customerPanel, null);
		customerPanel.setLayout(null);
		
		JButton btnCquit = new JButton("Quit");
		btnCquit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(EXIT_ON_CLOSE);
			}
		});
		btnCquit.setBounds(519, 248, 90, 25);
		customerGroupPanel.add(btnCquit);
		
		JButton btnCquit2 = new JButton("Quit");
		btnCquit2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(EXIT_ON_CLOSE);
			}
		});
		btnCquit2.setBounds(519, 248, 90, 25);
		customerPanel.add(btnCquit2);
		
		JLabel lblCreateFind = new JLabel("Create - Find - Update - Delete");
		lblCreateFind.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCreateFind.setBounds(54, 11, 193, 14);
		customerGroupPanel.add(lblCreateFind);
		
		JLabel lblCreateFind2 = new JLabel("Create - Find - Update - Delete");
		lblCreateFind2.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCreateFind2.setBounds(54, 11, 193, 14);
		customerPanel.add(lblCreateFind2);
		
		txtCgroupType = new JTextField();
		txtCgroupType.setBounds(137, 36, 110, 20);
		customerGroupPanel.add(txtCgroupType);
		txtCgroupType.setColumns(10);
		
		txtCdiscount = new JTextField();
		txtCdiscount.setColumns(10);
		txtCdiscount.setBounds(137, 67, 110, 20);
		customerGroupPanel.add(txtCdiscount);
		
		txtCid = new JTextField();
		txtCid.setColumns(10);
		txtCid.setBounds(137, 98, 110, 20);
		customerGroupPanel.add(txtCid);
		/////////////////////////////////////
		//CUSTOMER TXT FIELDS
		/////////////////////////////////////
		txtCfname = new JTextField();
		txtCfname.setBounds(137, 36, 110, 20);
		customerPanel.add(txtCfname);
		txtCfname.setColumns(10);
		
		txtClname = new JTextField();
		txtClname.setColumns(10);
		txtClname.setBounds(137, 67, 110, 20);
		customerPanel.add(txtClname);
		
		txtCaddress = new JTextField();
		txtCaddress.setColumns(10);
		txtCaddress.setBounds(137, 98, 110, 20);
		customerPanel.add(txtCaddress);
		
		txtCzipCode = new JTextField();
		txtCzipCode.setColumns(10);
		txtCzipCode.setBounds(137, 129, 110, 20);
		customerPanel.add(txtCzipCode);
		
		txtCcity = new JTextField();
		txtCcity.setBounds(137, 160, 110, 20);
		customerPanel.add(txtCcity);
		txtCcity.setColumns(10);
		
		txtCid2 = new JTextField();
		txtCid2.setColumns(10);
		txtCid2.setBounds(137, 191, 110, 20);
		customerPanel.add(txtCid2);
		
		JLabel lblCgroupType = new JLabel("Group type :");
		lblCgroupType.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCgroupType.setBounds(10, 39, 117, 14);
		customerGroupPanel.add(lblCgroupType);
		
		JLabel lblCdiscount = new JLabel("Discount :");
		lblCdiscount.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCdiscount.setBounds(10, 70, 117, 14);
		customerGroupPanel.add(lblCdiscount);
		
		JLabel lblSid = new JLabel("ID :");
		lblSid.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSid.setBounds(10, 101, 117, 14);
		customerGroupPanel.add(lblSid);
		
		////////////////////////////////////////
		//CUSTOMER LABELS
		////////////////////////////////////////
		
		JLabel lblFirstName = new JLabel("First Name :");
		lblFirstName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblFirstName.setBounds(10, 39, 117, 14);
		customerPanel.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name :");
		lblLastName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLastName.setBounds(10, 70, 117, 14);
		customerPanel.add(lblLastName);
		
		JLabel lblAddress = new JLabel("Address :");
		lblAddress.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAddress.setBounds(10, 101, 117, 14);
		customerPanel.add(lblAddress);
		
		JLabel lblZipCode = new JLabel("Zip Code :");
		lblZipCode.setHorizontalAlignment(SwingConstants.RIGHT);
		lblZipCode.setBounds(10, 132, 117, 14);
		customerPanel.add(lblZipCode);
		
		JLabel lblCity = new JLabel("City :");
		lblCity.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCity.setBounds(10, 163, 117, 14);
		customerPanel.add(lblCity);
		
		JLabel lblId = new JLabel("ID :");
		lblId.setHorizontalAlignment(SwingConstants.RIGHT);
		lblId.setBounds(10, 194, 117, 14);
		customerPanel.add(lblId);
		
		JButton btnCcreate2 = new JButton("Create");
		btnCcreate2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					customerCtrl.insertCustomer(Integer.parseInt(txtCgroupId2.getText()), txtCfname.getText(), txtClname.getText(),
					txtCaddress.getText(), Integer.parseInt(txtCzipCode.getText()), txtCcity.getText(), Integer.parseInt(txtCid2.getText()));
					JOptionPane.showMessageDialog(null,"Customer has been created.");
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in creating Customer!");
				}
			}
		});
		btnCcreate2.setBounds(341, 35, 110, 23);
		customerPanel.add(btnCcreate2);
		
		JButton btnCfind2 = new JButton("Find");
		btnCfind2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					Customer cust = customerCtrl.getCustomerID(txtCid2.getText());
					txtCfname.setText(cust.getFname());
					txtClname.setText(cust.getLname());
					txtCaddress.setText(cust.getAddress());
					txtCzipCode.setText(String.valueOf(cust.getZipCode()));
					txtCcity.setText(cust.getCity());
					txtCgroupId2.setText(String.valueOf(cust.getGroupId()));
				}
				catch(Exception r)
				{
					
				}
			}
		});
		btnCfind2.setBounds(341, 97, 110, 23);
		customerPanel.add(btnCfind2);
		
		JButton btnCdelete2 = new JButton("Delete");
		btnCdelete2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					customerCtrl.deleteCustomer(txtCid2.getText());
					JOptionPane.showMessageDialog(null,"Customer has been deleted.");
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in deleting Customer!");
				}
			}
		});
		btnCdelete2.setBounds(341, 222, 110, 23);
		customerPanel.add(btnCdelete2);
		
		JButton btnCupdate2 = new JButton("Update");
		btnCupdate2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					customerCtrl.updateCustomer(Integer.parseInt(txtCgroupId2.getText()), txtCfname.getText(), txtClname.getText(),
							txtCaddress.getText(), Integer.parseInt(txtCzipCode.getText()), txtCcity.getText(), Integer.parseInt(txtCid2.getText()));
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in updating Customer!");
				}
			}
		});
		btnCupdate2.setBounds(341, 159, 110, 23);
		customerPanel.add(btnCupdate2);
		
		txtCgroupId2 = new JTextField();
		txtCgroupId2.setColumns(10);
		txtCgroupId2.setBounds(137, 222, 110, 20);
		customerPanel.add(txtCgroupId2);
		
		JLabel lblGroupId = new JLabel("Group ID :");
		lblGroupId.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGroupId.setBounds(10, 225, 117, 14);
		customerPanel.add(lblGroupId);
		
		JButton btnClearFields = new JButton("Clear fields");
		btnClearFields.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearCustomerTextFields();
			}
		});
		btnClearFields.setBounds(137, 249, 110, 23);
		customerPanel.add(btnClearFields);
		
		JButton btnCcreate = new JButton("Create");
		btnCcreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
				customerGrCtrl.createCustomerGroup(Integer.parseInt(txtCid.getText()), txtCgroupType.getText(), Double.parseDouble(txtCdiscount.getText()));
				JOptionPane.showMessageDialog(null,"Customer Group has been created!");
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in creating Customer Group!");
				}
			}
		});
		btnCcreate.setBounds(341, 35, 110, 23);
		customerGroupPanel.add(btnCcreate);
		
		JButton btnCupdate = new JButton("Update");
		btnCupdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					customerGrCtrl.updateCustomerGroup(Integer.parseInt(txtCid.getText()), txtCgroupType.getText(), Double.parseDouble(txtCdiscount.getText()));
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in updating Customer Group!");
				}
			}
		});
		btnCupdate.setBounds(341, 159, 110, 23);
		customerGroupPanel.add(btnCupdate);
		
		JButton btnCdelete = new JButton("Delete");
		btnCdelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					customerGrCtrl.deleteCustomerGroup(txtCid.getText());
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in deleting Customer Group!");
				}
			}
		});
		btnCdelete.setBounds(341, 222, 110, 23);
		customerGroupPanel.add(btnCdelete);
		
		JButton btnCfind = new JButton("Find");
		btnCfind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					CustomerGroup custGr = customerGrCtrl.getCustomerGroupGroupType(txtCgroupType.getText());
					txtCdiscount.setText(String.valueOf(custGr.getDiscount()));
					txtCid.setText(String.valueOf(custGr.getId()));
	
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in finding Customer Group!");
				}
			}
		});
		btnCfind.setBounds(341, 97, 110, 23);
		customerGroupPanel.add(btnCfind);
		
		JButton button = new JButton("Clear fields");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearCustomerGroupTextFields();
			}
		});
		button.setBounds(137, 129, 110, 23);
		customerGroupPanel.add(button);
	}
	
	
	private void clearCustomerGroupTextFields()
	{
		txtCgroupType.setText("");
		txtCdiscount.setText("");
		txtCid.setText("");
	}
	
	private void clearCustomerTextFields()
	{
		txtCfname.setText("");
		txtClname.setText("");
		txtCaddress.setText("");
		txtCzipCode.setText("");
		txtCcity.setText("");
		txtCid2.setText("");
		txtCgroupId2.setText("");
	}

	
	public static boolean isNumeric(String _str)
	{
		try
		{
			long l = Long.parseLong(_str);
		}
		catch(NumberFormatException nfe)
		{
			return false;
		}
		return true;
	}
}
